package org.atlantis.grosbeak.depot;

import java.util.List;

import org.atlantis.grosbeak.depot.desc.ValueDesc;
import org.atlantis.grosbeak.depot.proxy.ObjectProxyMaker;
import org.atlantis.grosbeak.depot.proxy.ValueProxy;
import org.atlantis.grosbeak.depot.proxy.ValueProxyMaker;
import org.atlantis.grosbeak.json.Jsons;
import org.atlantis.grosbeak.lang.Exceptions;

/**
 * 此类作用是在Depot容器各个组件之间传递相关数据。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class DepotMaker {

	private String id;
	private ObjectProxyMaker objMaker;
	private Depot depot;
	private DepotEnv env;
	private List<ValueProxyMaker> vpms;

	/**
	 * 创建一个DepotMaker。
	 * 
	 * @param depot
	 *            Depot对象。
	 * @param env
	 *            DepotEnv对象。
	 * @param objMaker
	 *            ObjextProxyMaker对象。
	 * @param vpms
	 *            ValueProxyMaker对象集合。
	 * @param id
	 *            对象的ID。
	 */
	public DepotMaker(Depot depot, DepotEnv env, ObjectProxyMaker objMaker,
			List<ValueProxyMaker> vpms, String id) {
		this.id = id;
		this.objMaker = objMaker;
		this.depot = depot;
		this.env = env;
		this.vpms = vpms;
	}

	public Depot getDepot() {
		return depot;
	}

	public DepotEnv getEnv() {
		return env;
	}

	public String getId() {
		return id;
	}

	public ObjectProxyMaker getObjMaker() {
		return objMaker;
	}

	public DepotMaker clone(String id) {
		return new DepotMaker(depot, env, objMaker, vpms, id);
	}

	/**
	 * 根据对象中值的描述（ValueDesc）来创建一个值的代理对象（ValueProxy）。
	 * 
	 * @param vd
	 *            对象中值的描述。
	 * @return 值的代理对象。
	 */
	public ValueProxy makeValue(ValueDesc vd) {
		for (ValueProxyMaker vpm : vpms) {
			ValueProxy vp = vpm.make(this, vd);
			if (null != vp)
				return vp;
		}
		throw Exceptions.makeThrow(DepotException.class,
				"Unknow value {'%s':%s} for object '%s'", vd.getType(), Jsons
						.toJson(vd.getValue()), id);
	}
}
